#include "Board.h"
#include "GameManager.h"
#include <algorithm>
#include "SoundManager.h"

LetterTile::LetterTile(char letter, const CCPoint& pos, float fonSize, const char* fontName)
	:CCMenuItemImage()
	,mLetter(letter)
	,mImage(NULL)
	,mLableChar(NULL)		
{
	init();

	setNormalImage(CCSprite::create("tile_136.png"));
	setSelectedImage(CCSprite::create("tile_selected_160.png"));

	setScale(0.35f);
	setPosition(pos);
	setAnchorPoint(CCPoint(0.5f, 0.5f));
	ignoreAnchorPointForPosition(false);

	mLableChar = CCLabelTTF::create(&letter, fontName, fonSize);
	mLableChar->setAnchorPoint(ccp(0.5f,0.5f));
	mLableChar->setPosition(CCPoint(70.0f, 70.0f));
	mLableChar->setColor(ccBLACK);

	addChild(mLableChar);

	smAllTiles.insert(this);
}

extern CCLabelTTF* gLabel;

void LetterTile::selected()
{
	// the first selected tile, clear last time word
	if(smSelectedTilesRecord.size() == 0)
	{	
		if(gLabel)
		{
			gLabel->setString("");
			gLabel->setColor(ccWHITE);
		}
	}

	// selected already, do nothing
	if(smSelectedTilesRecord.find(this) != smSelectedTilesRecord.end())
	{
		return;
	}

	smSelectedTilesRecord.insert(this);

	CCMenuItem::selected();

	if (m_pNormalImage)
	{
		if (m_pDisabledImage)
		{
			m_pDisabledImage->setVisible(false);
		}

		if (m_pSelectedImage)
		{
			m_pNormalImage->setVisible(false);
			m_pSelectedImage->setVisible(true);
		}
		else
		{
			m_pNormalImage->setVisible(true);
		}
	}

	if(gLabel)
	{
		std::string word(gLabel->getString());
		word += mLetter;
		gLabel->setString(word.c_str());			
	}

	SoundManager::getInstance().playEffect(eSoundEffectID_Click1);
}

void LetterTile::recoverAllTiles()
{
	for(auto itAll = smSelectedTilesRecord.begin(); itAll != smSelectedTilesRecord.end(); itAll++)
	{
		(*itAll)->recover();
	}

	smSelectedTilesRecord.clear();
}

void LetterTile::recover()
{
	CCMenuItem::unselected();
	if (m_pNormalImage)
	{
		m_pNormalImage->setVisible(true);

		if (m_pSelectedImage)
		{
			m_pSelectedImage->setVisible(false);
		}

		if (m_pDisabledImage)
		{
			m_pDisabledImage->setVisible(false);
		}
	}
}


void Board::ccTouchEnded(CCTouch *touch, CCEvent* event)
{
	CC_UNUSED_PARAM(touch);
	CC_UNUSED_PARAM(event);
	CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchEnded] -- invalid state");

	checkAndRecordWord(gLabel->getString());

	LetterTile::recoverAllTiles();

	m_eState = kCCMenuStateWaiting;
}

void Board::checkAndRecordWord(const std::string& word)
{
	auto itRecorded = std::find(mRecordedWords.begin(), mRecordedWords.end(), word);
	if(itRecorded == mRecordedWords.end())
	{
		if(GameManager::getInstance().isWord(word))
		{
			mRecordedWords.push_back(word);

			SoundManager::getInstance().playEffect(eSoundEffectID_Correct1);

			if(gLabel)
			{
				gLabel->setColor(ccGREEN);
			}
		}
		else
		{
			SoundManager::getInstance().playEffect(eSoundEffectID_Error);

			if(gLabel)
			{
				gLabel->setColor(ccRED);
			}
		}
	}
	else
	{
		// add already
		SoundManager::getInstance().playEffect(eSoundEffectID_Repeat);

		if(gLabel)
		{
			gLabel->setColor(ccORANGE);
		}
	}
}